import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

var Login = () => import('./components/Login.vue');

var Home = () => import('./components/Home.vue');
var Welcome = () => import('./components/Welcome.vue');

var Users = () => import('./components/user/Users.vue');
var Rights = () => import(/* webpackChunkName: "power" */ './components/power/Rights.vue');
var Roles = () => import(/* webpackChunkName: "power" */ './components/power/Roles.vue');

var Categories = () => import(/* webpackChunkName: "goods" */ './components/goods/Categories.vue');
var Params = () => import(/* webpackChunkName: "goods" */ './components/goods/Params.vue');
var GoodsList = () => import(/* webpackChunkName: "goods" */ './components/goods/List.vue');
var AddGoods = () => import(/* webpackChunkName: "goods" */ './components/goods/Add.vue');
var Order = () => import('./components/order/Order.vue');
var Report = () => import('./components/report/Report.vue');


const routes = [
  {path: '/',redirect:'/login'},
  {path: '/login', name: 'login', component: Login},
  {path: '/home', name: 'Home',redirect:'/welcome',component: Home,children:[
      {path:'/welcome',name:'welcome',component:Welcome},
      {path:'/users',name:'users',component:Users},
      {path:'/rights',name:'rights',component:Rights},
      {path:'/roles',name:'roles',component:Roles},
      {path:'/categories',name:'categories',component:Categories},
      {path:'/params',name:'params',component:Params},
      {path:'/goods',name:'goods',component:GoodsList},
      {path:'/goods/add',name:'addGoods',component:AddGoods},
      {path:'/orders',name:'order',component:Order},
      {path:'/reports',name:'reports',component:Report},
    ]}
]

const router = new VueRouter({routes});

/*路由导航守卫*/
router.beforeEach((to,from,next)=>{
  if(to.path === '/login') return next();
  /*读取 seesionStorage 中的token信息*/
  const tokenStr=window.sessionStorage.getItem('token');
  if(!tokenStr){
    /*没有token强制跳转到登陆页*/
    return next('/login');
  }
  next();
});
export default router
